package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.databinding.ActivitySelPicBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelPicActivity extends BaseAc<ActivitySelPicBinding> {

    private SelPictureAdapter selPictureAdapter;
    private List<SelectMediaEntity> listSel = new ArrayList<>();//选择的集合

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_pic;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSelPicBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvProPicStart.setOnClickListener(this);

        mDataBinding.rvSelPicList.setLayoutManager(new GridLayoutManager(mContext, 3));
        selPictureAdapter = new SelPictureAdapter();
        mDataBinding.rvSelPicList.setAdapter(selPictureAdapter);
        selPictureAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvProPicStart) {
            if (listSel.size() == 0) {
                ToastUtils.showShort(getString(R.string.please_sel_pic));
                return;
            }
            ProPicActivity.listPro = listSel;
            startActivity(ProPicActivity.class);
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (selPictureAdapter.getItem(position).isChecked()) {
            selPictureAdapter.getItem(position).setChecked(false);
            listSel.remove(selPictureAdapter.getItem(position));
            selPictureAdapter.notifyDataSetChanged();
        } else {
            listSel.add(selPictureAdapter.getItem(position));
            selPictureAdapter.getItem(position).setChecked(true);
            selPictureAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initData() {
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    selPictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvSelPicList.setVisibility(View.VISIBLE);
                    mDataBinding.ivSelPicNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelPicList.setVisibility(View.GONE);
                    mDataBinding.ivSelPicNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}